package hikari.gui.guicomponents;

import com.gooddies.persistence.Properties;
import com.gooddies.wiring.annotations.PostWiring;
import com.gooddies.wiring.annotations.Wire;
import com.gooddies.wiring.annotations.WiringComponent;
import hikari.utils.Utils;
import java.awt.Component;
import java.awt.HeadlessException;
import java.io.File;
import javax.swing.Icon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileView;

/**
 * @author sad
 */
@WiringComponent
public class ProjectsFileChooser extends JFileChooser {

    private final static String HIKARI_EXTENSION = ".hikari";
    private final static String persistanceKey = "project.file.chooser";
    private Icon programIcon;
    @Wire
    private Utils utils;

    private ProjectsFileChooser() {
    }

    @PostWiring
    private void init() {
        if (Properties.get().hasKey(persistanceKey)) {
            String folder = Properties.get().getString(persistanceKey);
            if (!folder.endsWith(".")) {
                folder += ".";
            }
            setSelectedFile(new File(folder));
        }
        setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        programIcon = utils.loadIconFromResource("programmIcon.png");
        setFileView(new FileView() {
            @Override
            public Icon getIcon(File f) {
                if (f.isDirectory()) {
                    String projectPath = utils.combinePath(f.getAbsolutePath(), "game.hikari");
                    if (new File(projectPath).exists()) {
                        return programIcon;
                    }
                }
                return super.getIcon(f); //To change body of generated methods, choose Tools | Templates.
            }
        });

        /*setFileFilter(new FileFilter() {
         @Override
         public boolean accept(File f) {
         if (isHikariFile(f.getName())) {
         return true;
         }
         if (f.isDirectory()) {
         return true;
         }
         return false;
         }

         @Override
         public String getDescription() {
         return String.format("Hikari project folder");
         }
         });*/
    }

    @Override
    public int showOpenDialog(Component parent) throws HeadlessException {
        int result = super.showOpenDialog(parent);
        if (result == APPROVE_OPTION) {
            saveFolder();
        }
        return result;
    }

    @Override
    public int showSaveDialog(Component parent) throws HeadlessException {
        int result = super.showSaveDialog(parent);
        if (result == APPROVE_OPTION) {
            saveFolder();
        }
        return result;
    }

    private void saveFolder() {
        String folder = utils.getFilePath(getSelectedFile().getAbsolutePath());
        Properties.get().putString(persistanceKey, folder);
    }

    private boolean isHikariFile(String path) {
        return path.endsWith(HIKARI_EXTENSION);
    }

    public String getSelectedPath() {
        String path = getSelectedFile().getAbsolutePath();
        return utils.combinePath(path, "game.hikari");
//        if(isHikariFile(path)){
//            return path;
//        }else{
//            return path+HIKARI_EXTENSION;
//        }
    }
}
